package cn.mrlong.base.utils.recordphone;

import android.annotation.SuppressLint;
import android.app.Service;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.os.IBinder;
import android.provider.CallLog;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

public class PhoneService extends Service {
    private TelephonyManager telephonyManager = null;
    private String outgoingnumber = null;
    ;
    private RecordAudioUtil recordAudioUtil = null;
    private Intent intent = null;
    private boolean isDebug = true;

    public PhoneService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        throw new UnsupportedOperationException("Not yet implemented");
    }

    @Override
    public void onCreate() {
        this.telephonyManager = (TelephonyManager) super.getSystemService(Context.TELEPHONY_SERVICE);
        this.telephonyManager.listen(new PhoneStateListenerlmpl(), PhoneStateListener.LISTEN_CALL_STATE);
        LogMsg("启动录音服务");
        super.onCreate();
    }

    @SuppressWarnings("deprecation")
    @Override
    public void onStart(Intent intent, int startId) {
        //this.outgoingnumber = intent.getStringExtra("outgoingnumber");
        //LogMsg("获得录音电话" + outgoingnumber);
        super.onStart(intent, startId);
    }

    private class PhoneStateListenerlmpl extends PhoneStateListener {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {

            switch (state) {
                case TelephonyManager.CALL_STATE_IDLE:
                    if (PhoneService.this.recordAudioUtil != null) {
                        PhoneService.this.recordAudioUtil.stop();
                        PhoneService.this.recordAudioUtil = null;
                    }
                    LogMsg("挂断");
                    break;
                case TelephonyManager.CALL_STATE_RINGING:
                    LogMsg("来电");
                    //PhoneService.this.recordAudioUtil = new RecordAudioUtil(incomingNumber, "incomingcall");
                    //PhoneService.this.recordAudioUtil.record();
                    break;
                case TelephonyManager.CALL_STATE_OFFHOOK:
                    LogMsg("接起电话时");
                    PhoneService.this.recordAudioUtil = new RecordAudioUtil(new RecordListener() {
                        @Override
                        public void returnSuccFilePath(String path, String fileSize) {
                            //组装对象

                        }
                    });
                    PhoneService.this.recordAudioUtil.record();
                    break;
            }
            super.onCallStateChanged(state, incomingNumber);
        }
    }

    private boolean getCallLogState(String mobileNumber) {
        ContentResolver cr = getContentResolver();
        boolean isLink = false;
        @SuppressLint("MissingPermission") final Cursor cursor = cr.query(CallLog.Calls.CONTENT_URI,
                new String[]{CallLog.Calls.NUMBER, CallLog.Calls.TYPE, CallLog.Calls.DURATION},
                CallLog.Calls.NUMBER + "=? and " + CallLog.Calls.TYPE + "= ?",
                new String[]{mobileNumber, "CallLog.Calls.TYPE"}, null);
        while (cursor.moveToNext()) {
            int durationIndex = cursor.getColumnIndex(CallLog.Calls.DURATION);
            long durationTime = cursor.getLong(durationIndex);
            if (durationTime > 0) {
                isLink = true;
            } else {
                isLink = false;
            }
        }
        return isLink;
    }

    public String getFileName(String url) {
        if (url.length() <= 2) {
            return null;
        }
        String name = url.substring(url.lastIndexOf("/") + 1, url.lastIndexOf("."));
        if (null != name) {
            return name;
        }
        return null;
    }

    @Override
    public void onDestroy() {
        LogMsg("销毁");
        super.onDestroy();
        if (PhoneService.this.recordAudioUtil != null) {
            PhoneService.this.recordAudioUtil.stop();
            PhoneService.this.recordAudioUtil = null;
        }
    }

    public void LogMsg(String msg) {
        if (isDebug)
            Log.e("PhoneService = >", msg);
    }
}
